from pynput.keyboard import Key, Controller  # 导入按键控制
import time  # 导入时间包，调用sleep函数让琴键有时间间隔
import threading  # 导入线程包
from pywinauto import Desktop


# 演奏函数
def play_piano(music, keytime):
    for n in music:
        if n.isupper():
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press(n.lower())
            time.sleep(keytime - 0.001)
            keyboard.release(n.lower())
            keyboard.release(Key.shift)
        elif n == "|" or n == ")":
            pass
        elif n in "!@$%^*(":
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press("1245689"["!@$%^*(".index(n)])
            time.sleep(keytime - 0.001)
            keyboard.release("1245689"["!@$%^*(".index(n)])
            keyboard.release(Key.shift)
        elif n != " " and n != "-":
            keyboard.press(n)
            if music.index(n) != len(music) - 1 and music[music.index(n) + 1] == ")":
                time.sleep(keytime / 2)
            else:
                time.sleep(keytime)
            keyboard.release(n)
        elif n == "-":
            time.sleep(2 * keytime)
        else:
            time.sleep(keytime)


def thread_play(play_piano, keytime, right="", left="", rightThumb="", rightIndexFinger="", leftThumb="",
                leftIndexFinger=""):
    # 运行线程
    # 运行线程
    rt = threading.Thread(target=play_piano, args=(right, keytime))  # 主旋律线程
    lt = threading.Thread(target=play_piano, args=(left, keytime))  # 和弦线程
    rtt = threading.Thread(target=play_piano, args=(rightThumb, keytime))  # 右手拇指线程
    rift = threading.Thread(target=play_piano, args=(rightIndexFinger, keytime))  # 右手食指线程
    ltt = threading.Thread(target=play_piano, args=(leftThumb, keytime))  # 左手拇指线程
    lift = threading.Thread(target=play_piano, args=(leftIndexFinger, keytime))  # 右手食指线程
    rt.start()
    lt.start()
    rtt.start()
    rift.start()
    ltt.start()
    lift.start()
    rt.join()
    lt.join()
    rtt.join()
    rift.join()
    ltt.join()
    lift.join()


if __name__ == '__main__':
    # 准备演奏
    # 控制键盘
    keyboard = Controller()
    # 切换到vue键盘钢琴(auto piano)网页
    keyboard.press(Key.cmd)
    time.sleep(1)
    keyboard.press("d")
    keyboard.release("d")
    keyboard.release(Key.cmd)
    # 链接的方式点击桌面任务栏的正在运行程序print_control_identifiers()
    dlg = Desktop(backend="uia").任务栏.运行中的程序.child_window(title="Google Chrome - 1 个运行窗口", auto_id="Chrome",
                                                         control_type="Button").click()
    time.sleep(2)
    keyboard.press(Key.f11)
    keyboard.release(Key.f11)
    right = "--fdfh|f ---|d-dfdfh|d- -as|" \
            "s -fdfh|f ---|d-dfdfh|d- -as|"
    left = "----|qtyup -|wyopa -|89yua -|" \
           "eurto- |qtyup -|wyopad-|89yua -|"
    thread_play(play_piano, 0.35, right, left)
    right = "----| pppppp |p oya -| ooooooo|" \
            "d-s--| ppppppp|o-dasdh| sahdshd|"
    left = "eurto- |qtyu--|wy p--|8wyu9 -|" \
           "eurto- |qtyu--|wyopa -|8wyu--|"
    rightThumb = "----| ff  ff |f ---| dd  dd |" \
                 "----| ff  ffj|f ---|----|"
    thread_play(play_piano, 0.35, right, left, rightThumb)
    right = "shdsxzxv|xgjxgjxz|dhkdzlzv|zfhzfhkl|" \
            "sfhklkjh|fipfipfd|--dsks|ou a u u|" \
            " s-sklz|"
    left = "eurto- |qtyup -|wyopa -|6wyua -|" \
           "eutyo- |qtyu--|wyopa -|8wyu r-|" \
           "e rto- |"
    rightThumb = "----|----|z ---|----|" \
                 "l --|----|----|----|" \
                 "----|"
    thread_play(play_piano, 0.35, right, left, rightThumb)
    right = "xgjxgjxgjxgjxgjx|zdhzdhzdhzdhzdhk|" \
            "zfhzfhzfhzfhzfhk|zlklkhfghsghjklz|" \
            "xgjxgjxgjxgjxgjx|zdhzdhzdhzdhzdhk|" \
            "zfhzfhzfhzfhzfhk|zlklkhfghsghjklz|"
    left = "q t y u p y u p|w y o p a y o a|" \
           "8 w y u a y u p|e u r t e r t o|" \
           "q t y u p y u p|w y o p a y o a|" \
           "8 w y u a y u p|e u r t e r t o|"
    thread_play(play_piano, 0.2, right, left)
    right = "l k j h |e ---|7 --f |d f h -|" \
            "w ---|"
    left = "x x l k |t ---|9 ---|----|" \
           "y ---|"
    rightThumb = "m n b v |u ---|r ---|8 ---|"
    rightIndexFinger = "e -6 -|q ---|5 ---|"
    leftThumb = "----|----|y ---|"
    thread_play(play_piano, 0.3, right, left, rightThumb, rightIndexFinger, leftThumb)
    keyboard.press(Key.f11)
    keyboard.release(Key.f11)
